//
//  PTRateView.m
//  PlanTech
//
//  Created by ruifeng song on 2018/7/25.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "PTRateView.h"
#import "XHStarRateView.h"

@interface  PTRateView()

@property(nonatomic,strong)UILabel * nameLabel ;
@property(nonatomic,strong)UIImageView *headerImageView;
@property(nonatomic,strong)UIView * rateAlertView;

@property(nonatomic,strong)XHStarRateView * starRateView;

@end

@implementation PTRateView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        
        [self addSubview:self.rateAlertView];
        
        UIImageView * backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.rateAlertView.width, self.rateAlertView.height - 45)];
        backImageView.userInteractionEnabled = YES;
        [backImageView setImage:[UIImage imageNamed:@"doodles"]];
        [self.rateAlertView addSubview:backImageView];
        
        UIButton *ratingButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [ratingButton setTitle:@"确定" forState:UIControlStateNormal];
        ratingButton.titleLabel.font = PTFONT_18;
        [ratingButton setFrame:CGRectMake(0, backImageView.bottom, self.rateAlertView.width, 45)];
        [ratingButton setTitleColor:PTColor_2e2e2e forState:UIControlStateNormal];
        [ratingButton addTarget:self action:@selector(onClickRatingButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.rateAlertView addSubview:ratingButton];
        
        UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 27, self.rateAlertView.width, 15)];
        lbl.font = PTFONT_15;
        lbl.text =@"请对本次服务进行评价";
        lbl.textColor = [UIColor whiteColor];
        lbl.textAlignment = NSTextAlignmentCenter;
        [backImageView addSubview:lbl];
        
        _headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.rateAlertView.width - 80)/2, lbl.bottom + 18, 80, 80)];
        _headerImageView.layer.cornerRadius = 40;
        [_headerImageView setImage:[UIImage imageNamed:@"list_header_default_img"]];
        _headerImageView.layer.masksToBounds =YES;
        [backImageView addSubview:_headerImageView];
        
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _headerImageView.bottom + 15, self.rateAlertView.width,15)];
        _nameLabel.text = @"张改改";
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.font = PTFONT_15;
        _nameLabel.textColor = [UIColor whiteColor];
        [backImageView addSubview:_nameLabel];
        
        CGFloat leftSpacing = (self.rateAlertView.width - 205)/2;
        
        _starRateView = [[XHStarRateView alloc]initWithFrame:CGRectMake(leftSpacing, _nameLabel.bottom + 20, 205, 29) numberOfStars:5 selectNumberStars:0 rateStyle:WholeStar isAnination:YES isTouch:YES finish:^(CGFloat currentScore) {
            
        }];
        [backImageView addSubview:_starRateView];
    }
    return self;
}

-(void)onClickRatingButton:(id)sender
{
    if (self.delegate) {
        
        self.rating = self.starRateView.currentScore;
        [self.delegate submitRatingInfo:self];
    }
}

-(UIView *)rateAlertView
{
    if (_rateAlertView ==nil) {
    
        _rateAlertView = [[UIView alloc]initWithFrame:CGRectMake((self.frame.size.width- 245)/2,(self.frame.size.height - 301)/2,245,301)];
        _rateAlertView.layer.cornerRadius = 10;
        _rateAlertView.backgroundColor = [UIColor whiteColor];
        _rateAlertView.userInteractionEnabled = YES;
    }
    
    return _rateAlertView;
}

-(void)setMyListModel:(LifeServiceMyListModel *)myListModel
{
    _myListModel = myListModel;
    
    if (myListModel.receiver.avatar && myListModel.receiver.avatar.length > 0) {
        
        [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:myListModel.receiver.avatar] placeholderImage:[UIImage imageNamed:@"list_header_default_img"]];
    }
    else
    {
        [self.headerImageView setImage:[UIImage imageNamed:@"list_header_default_img"]];
    }
    
    self.nameLabel.text = myListModel.receiver.name;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
